package com.itheima.edu.info.manager.entry;

import com.itheima.edu.info.manager.controller.StudentController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;

//项目的启动引擎类：未来整个项目的启动入口就放在这个类里
public class ClientEntry {
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientEntry.class);

    public static void main(String[] args) throws IOException {
        //只负责一级菜单的展示
        Scanner sc = new Scanner(System.in);
        //注册
        System.out.println("--------欢迎来到黑马信息管理系统--------");
        System.out.println("请输入您的选择: 1.注册  2.登录  3.退出");
        String choose = sc.next();
        switch (choose) {
            case "1":
                Registor(sc);

            case "2":
                //验证账号密码
                login(sc);
                break;
            case "3":
                System.out.println("感谢您的使用");
                System.exit(0);
            default:
                System.out.println("您的输入有误, 请重新输入");
                break;
        }


        ll:
        while (true) {
            // 主菜单搭建
            System.out.println("--------欢迎来到黑马信息管理系统--------");

            System.out.println("请输入您的选择: 1.学生管理  2.老师管理  3.退出黑马信息管理系统");
            String choice = sc.next();
            LOGGER.info("正在连接学生管理系统服务端...");
            switch (choice) {
                case "1":
                    // 开启学生管理系统,创建学生管理系统客服对象,提供服务
                    StudentController studentController = new StudentController();
                    studentController.start();
                    break;
                case "2":
                    System.out.println("老师管理");
                    break;
                case "3":
                    System.out.println("感谢您的使用");
                    // 退出当前正在运行的JVM虚拟机
                    System.exit(0);//0: 表示让虚拟机安心的走...非0也可以,不过走的不安心
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }

    }

    private static void Code(Scanner sc) throws IOException {
        //验证码
        while (true) {
            LOGGER.info("连接服务端，请求验证码");
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[0]");
            bw.flush();
            socket.shutdownOutput();
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            LOGGER.info("已接收到服务端的验证码");
            System.out.println("请输入验证码：" + s);
            socket.close();
            String code = sc.next();
            if (code.equalsIgnoreCase(s)) {
                System.out.println("验证成功");
                break;
            } else {
                System.out.println("验证失败,请重新输入");
            }
        }
    }

    //登录
    private static void login(Scanner sc) throws IOException {
        lo:
        while (true) {
            Properties prop = new Properties();
            FileReader fr = new FileReader("studentServer\\prop.properties");
            System.out.println("请输入您要登录的管理员账号");
            String username = sc.next();
            System.out.println("请输入您的密码");
            String password = sc.next();
            prop.load(fr);
            fr.close();
            Set<Object> keySet = prop.keySet();
            for (Object key : keySet) {
                Object value = prop.get(key);
                if (key.equals(username) && value.equals(password)) {
                    Code(sc);
                    System.out.println("欢迎管理员：" + username + "!");
                    break lo;
                }/*else {
                    System.out.println("登录失败");
                    break ;
                }*/
            }
        }
    }

    //注册
    private static void Registor(Scanner sc) throws IOException {
        System.out.println("请输入注册用户名");
        String username1 = sc.next();
        while (true) {
            if (username1.length() < 6 || username1.length() > 10) {
                System.out.println("用户名长度不合规，请重新输入6-10位的用户名");
                username1 = sc.next();
            } else {
                break;
            }
        }
        System.out.println("请输入注册密码");
        String password1 = sc.next();
        while (true) {
            if (password1.length() < 6 || username1.length() > 10) {
                System.out.println("密码太短，请重新输入6-10位的密码");
                password1 = sc.next();
            } else {
                break;
            }
        }
        Socket socket = getSocket();
        BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw1.write("[6]" + "," + username1 + "," + password1);
        bw1.flush();
        socket.shutdownOutput();
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String res = br.readLine();
        //3. 根据业务员反馈的结果,输出提示信息到控制台
        if (res.equals("true")) {
            Code(sc);
            System.out.println("注册成功");
        } else {
            System.out.println("注册失败");
        }
    }

    private static Socket getSocket() throws IOException {
        return new Socket("127.0.0.1", 10000);
    }
}
